package com.myk.game.lib.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;

import com.myk.game.lib.R;


/**
 * 全屏Dialog，用于代替Activity和Fragment
 */
public abstract class FullScreenDialog extends Dialog {

    private final int layoutRes;
    private final int backViewId;

    public FullScreenDialog(@NonNull Context context, @LayoutRes int layoutRes, @IdRes int backViewId) {
        super(context, R.style.style_full_screen_dialog);
        this.layoutRes = layoutRes;
        this.backViewId = backViewId;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setCancelable(false);

        Window window = getWindow();
        if (window != null) {
            window.getDecorView().setPadding(0, 0, 0, 0);
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
            layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
            window.setAttributes(layoutParams);
        }
        View contentView = LayoutInflater.from(getContext()).inflate(layoutRes, null);
        setContentView(contentView);
        findViewById(backViewId).setOnClickListener(v -> cancel());
        init(contentView);
    }

    protected abstract void init(View contentView);

    public View getBackView(){
        return findViewById(backViewId);
    }

    public void showBackView(){
        findViewById(backViewId).setVisibility(View.VISIBLE);
    }

    public void hideBackView(){
        findViewById(backViewId).setVisibility(View.GONE);
    }

}
